import com.alibaba.fastjson.JSON;
import com.aliyun.odps.udf.UDF;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpliderRatzillaGoods extends UDF {
    /*public static void main(String[] args) {
            SpliderRatzillaGoods sg = new SpliderRatzillaGoods();

            String s1 = "";
            String evaluate = sg.evaluate(s1);
            System.out.println(evaluate);
        }*/

    public String evaluate(String html) {
        Map<String, Object> map = new HashMap<>();
        try {
            /*Connection con = Jsoup.connect("https://www.ratzillacosme.com/bb/pdc-direct-white-dew-whitening-foundation/");
            Document doc = con.get();*/
            Document doc = Jsoup.parse(html);
            String title = doc.select(".entry-title").get(0).text().trim();
            map.put("title", title);
            /*Elements as = doc.getElementsByClass("mh-breadcrumb").get(0).getElementsByTag("a");
            StringBuffer bd=new StringBuffer();
            for (Element element:as){
                bd.append(element.text().trim()).append("、");
            }
            if (StringUtils.isNotBlank(bd.toString())){
                map.put("breadcrumb", bd.substring(0,bd.length()-1));
            }*/
            int pSize = 0;
            if (getFlag(doc, "su-accordion", 0)) {
                Element accordion = doc.getElementsByClass("su-accordion").get(0);
                //System.out.println(accordion);
                pSize = accordion.getElementsByTag("p").size();
                for (int a = 0; a < accordion.getElementsByClass("su-spoiler-title").size(); a++) {
                    String key = accordion.getElementsByClass("su-spoiler-title").get(a).text();
                    //System.out.println(key);
                    System.out.println(accordion.getElementsByClass("su-spoiler-content").get(a));
                    //System.out.println(value);
                    if (key.toUpperCase().contains("DESCRIPTION")) {
                        key = "Description";
                    } else if (key.toUpperCase().contains("INGREDIENT")) {
                        key = "Ingredients";
                    } else if (key.toUpperCase().contains("日本")) {
                        key = "Japanese";
                    }
                    Element valueContent = accordion.getElementsByClass("su-spoiler-content").get(a);
                    String value = "";
                    StringBuffer sb = new StringBuffer();
                    //成分特殊处理
                    if (StringUtils.equals(key, "Ingredients")) {
                        List<Map<String, String>> inredientList = new ArrayList<>();
                        if (getFlag(valueContent, "p", 0)) {
                            Elements ps = valueContent.getElementsByTag("p");
                            for (Element p : ps) {
                                String text = p.text().trim();
                                Map<String, String> ingreMap = new HashMap<>();
                                if (StringUtils.isNotBlank(text) && text.contains(":")) {
                                    ingreMap.put("goods_name", text.substring(0, text.indexOf(":")));
                                    ingreMap.put("goods_ingredient", text.substring(text.indexOf(":") + 1));
                                } else {
                                    ingreMap.put("goods_name", "");
                                    ingreMap.put("goods_ingredient", text);
                                }
                                inredientList.add(ingreMap);
                            }
                        } else {
                            Map<String, String> ingreMap = new HashMap<>();
                            ingreMap.put("goods_name", "");
                            ingreMap.put("goods_ingredient", valueContent.text().trim());
                            inredientList.add(ingreMap);
                        }
                        map.put(key, JSON.toJSONString(inredientList));
                    } else {
                        if (getFlag(valueContent, "p", 0)) {
                            Elements ps = valueContent.getElementsByTag("p");
                            for (Element p : ps) {
                                sb.append(p.text().trim()).append("@!@!");
                            }
                        } else {
                            sb.append(valueContent.text().trim()).append("@!@!");
                        }
                        map.put(key, sb.substring(0, sb.length() - 4));
                    }
                }
            } else if (getFlag(doc, "su-tabs su-tabs-style-1", 0)) {
                Element su_tabs = doc.getElementsByClass("su-tabs su-tabs-style-1").get(0);
                pSize = su_tabs.getElementsByTag("p").size();
                Element su_tabs_nav = su_tabs.getElementsByClass("su-tabs-nav").get(0);
                Element su_tabs_panes = su_tabs.getElementsByClass("su-tabs-panes").get(0);
                for (int a = 0; a < su_tabs_nav.getElementsByTag("span").size(); a++) {
                    String key = su_tabs_nav.getElementsByTag("span").get(a).text();
                    if (key.toUpperCase().contains("DESCRIPTION")) {
                        key = "Description";
                    } else if (key.toUpperCase().contains("INGREDIENT")) {
                        key = "Ingredients";
                    } else if (key.toUpperCase().contains("日本")) {
                        key = "Japanese";
                    }
                    Element valueContent = su_tabs_panes.getElementsByTag("div").get(a + 1);
                    StringBuffer sb = new StringBuffer();
                    //成分特殊处理
                    if (StringUtils.equals(key, "Ingredients")) {
                        List<Map<String, String>> inredientList = new ArrayList<>();
                        if (getFlag(valueContent, "p", 0)) {
                            Elements ps = valueContent.getElementsByTag("p");
                            for (Element p : ps) {
                                String text = p.text().trim();
                                Map<String, String> ingreMap = new HashMap<>();
                                if (StringUtils.isNotBlank(text) && text.contains(":")) {
                                    ingreMap.put("goods_name", text.substring(0, text.indexOf(":")));
                                    ingreMap.put("goods_ingredient", text.substring(text.indexOf(":") + 1));
                                } else {
                                    ingreMap.put("goods_name", "");
                                    ingreMap.put("goods_ingredient", text);
                                }
                                inredientList.add(ingreMap);
                            }
                        } else {
                            Map<String, String> ingreMap = new HashMap<>();
                            ingreMap.put("goods_name", "");
                            ingreMap.put("goods_ingredient", valueContent.text().trim());
                            inredientList.add(ingreMap);
                        }
                        map.put(key, inredientList);
                    } else {
                        if (getFlag(valueContent, "p", 0)) {
                            Elements ps = valueContent.getElementsByTag("p");
                            for (Element p : ps) {
                                sb.append(p.text().trim()).append("@!@!");
                            }
                        } else {
                            sb.append(valueContent.text().trim()).append("@!@!");
                        }
                        map.put(key, sb.substring(0, sb.length() - 4));
                    }




                    /*String value = su_tabs_panes.getElementsByTag("div").get(a + 1).text();
                    System.out.println(su_tabs_panes.getElementsByTag("div").get(a + 1));
                    map.put(key, value);*/
                }
            }
            //System.out.println(map);
            if (getFlag(doc, "specs", 0)) {
                Element specs = doc.select(".specs").get(0);
                for (int a = 0; a < specs.getElementsByTag("dt").size(); a++) {
                    String key = specs.getElementsByTag("dt").get(a).text();
                    if (key.toUpperCase().contains("SPF")) {
                        key = "SPF";
                    }
                    if (key.toUpperCase().contains("UV Filter".toUpperCase())) {
                        key = "uv_filter";
                    }
                    System.out.println(key);
                    String value = specs.getElementsByTag("dd").get(a).text();
                    map.put(key, value);
                }
            } else if (getFlag(doc, "entry-content clearfix", 0)) {
                //System.out.println();
                Elements ps = doc.getElementsByClass("entry-content clearfix").get(0).getElementsByTag("p");
                for (Element p : ps) {
                    if (p.toString().contains("<strong>Brand:") || p.toString().contains("<strong>Price:") || p.toString().contains("<strong>Producer:")) {
                        p.getElementsByTag("em").remove();
                        System.out.println(p.text().trim());
                        Elements strong = p.getElementsByTag("strong");
                        StringBuffer sb = new StringBuffer();
                        for (Element element : strong) {
                            if (StringUtils.isNotBlank(element.text())) {
                                sb.append(element.text().trim()).append("|");
                            }
                        }
                        String re = sb.substring(0, sb.length() - 1);
                        //System.out.println(re);
                        String[] split = p.text().trim().split(re);
                        /*System.out.println(split.length);
                        for (String s:split){
                            System.out.println(s);
                        }*/
                        List<String> keyList = new ArrayList<>();
                        for (int i = 0; i < strong.size(); i++) {
                            String key = strong.get(i).text().replace(":", "").trim();
                            if (!keyList.contains(key) && StringUtils.isNotBlank(key)) {
                                keyList.add(key);
                            }
                        }
                        System.out.println(keyList);
                        for (int i = 0; i < keyList.size(); i++) {
                            String key = keyList.get(i);
                            if (key.toUpperCase().contains("SPF")) {
                                key = "SPF";
                            }
                            if (key.toUpperCase().contains("UV Filter".toUpperCase())) {
                                key = "uv_filter";
                            }
                            String value = split[i + 1].trim();
                            map.put(key, value);
                        }
                    }
                }
                //
                //int len = p.size() - pSize - 1;
                //System.out.println(len);
                //System.out.println(pSize);
                //System.out.println(p);
                /*for (int a = 1; a < p.size(); a++) {
                    p.get(a).getElementsByTag("em").remove();
                    String text = p.get(a).text();
                    System.out.println(p);
                    StringBuffer sb = new StringBuffer();
                    if (text.contains("RatzillaCosme Approved")){
                        continue;
                    }
                   // System.out.println(JSON.toJSONString(map));
                    if (text.contains("UV Filter(s):")) {
                        map.put("uv_filter", text);
                    } else if (text.contains("SPF")) {
                        map.put("SPF", text);
                    } else if (StringUtils.isNotBlank(text)&&p.get(a).toString().contains("strong")) {
                        Elements strong = p.get(a).getElementsByTag("strong");
                        if(strong.size()==0){
                            strong= p.get(a+1).getElementsByTag("strong");
                        }
                        if (strong.size() > 0) {
                            for (int i = 0; i < strong.size(); i++) {
                                String text1 = strong.get(i).text();
                                //System.out.println(text1);
                                if (!sb.toString().contains(text1)){
                                    sb.append(text1).append("|");
                                }
                            }
                            String re = sb.substring(0, sb.length() - 1);
                            //System.out.println(re);
                            String[] split = text.split(re);
                            //System.out.println(split.length);
                            //System.out.println(strong.size());
                            List<String> keyList=new ArrayList<>();
                            for (int i = 0; i < strong.size(); i++) {
                                String key = strong.get(i).text().replace(":", "").trim();
                                if (!keyList.contains(key)){
                                    keyList.add(key);
                                }
                            }
                            System.out.println(keyList);
                            for (int i=0;i<keyList.size();i++){
                                String key =keyList.get(i);
                                String value =split[i+1].trim();
                                map.put(key,value);
                            }
                        }
                    }
                }*/
            }
            //System.out.println(map);
            if (null != map && map.size() > 0) {
                return JSON.toJSONString(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "解析错误" + e.getMessage();
        }
        return null;
    }

    private static boolean getFlag(Document doc, String className, int index) {
        try {
            doc.getElementsByClass(className).get(index);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private static boolean getFlag(Element element, String tagName, int index) {
        try {
            element.getElementsByTag(tagName).get(index);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
